package com.chis.sms.main;

import com.chis.sms.smsServer.SmsServerProxy;
import com.chis.util.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年07月13日
 */
public class Testmain {
    private static final String _url="http://10.88.77.59:7001/SmsService/SmsService?wsdl";
    private static final String _use="cybercdc";
    private static final String _psd="000000";
//    private static final String _url="http://appcdc.chinaymt.cn:18889/SmsService/SmsService?wsdl";
//    private static final String _use="jssjk";
//    private static final String _psd="000000";
    private static final String _sign="";

    public static void main(String[] args) {
        Testmain.sendmymsg("18951278941","wlj测试");
    }

    public static void sendmymsg(String phoneNum,String content) {
        String sendUrl = _url;
        String username = _use;
        String password = _psd;
        String sign = _sign;
        SmsServerProxy proxy = new SmsServerProxy(sendUrl);

        StringBuilder sb = new StringBuilder();
        if(StringUtils.isNotBlank(phoneNum) && StringUtils.isNotBlank(content)) {
            sb.append("<?xml version='1.0' encoding='GB2312'?>");
            sb.append("<SmsInfoes>");
            for (String phone : phoneNum.split(",")) {
                sb.append("<SmsInfo>");
                sb.append("<UserCode>").append(username).append("</UserCode>");
                sb.append("<TelNum>").append(phone).append("</TelNum>");
                sb.append("<Content>").append(content).append("</Content>");
                sb.append("<Count>1</Count>");
                sb.append("<InvalidDate>").append("2020-07-25 00:00:00").append("</InvalidDate>");
                sb.append("<SpTag>0</SpTag>");

                sb.append("<Sign>").append(sign).append("</Sign>");
                sb.append("<SystemMark>12</SystemMark>");
                sb.append("<IsNight>1</IsNight>");
                sb.append("<NetTag>1</NetTag>");
                sb.append("</SmsInfo>");
            }
            sb.append("</SmsInfoes>");
            try {
                int rst = proxy.smsUpLoad(compress(sb.toString()), username, new MD5Util().getMD5ofStr(password).toLowerCase());
                System.out.println(rst);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static String compress(String s) {
        try {
            ByteArrayInputStream input = new ByteArrayInputStream(s.getBytes("GBK"));
            ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
            GZIPOutputStream gzout = new GZIPOutputStream(output);

            byte[] buf = new byte[1024];
            int number;

            while ((number = input.read(buf)) != -1) {
                gzout.write(buf, 0, number);
            }

            gzout.close();
            input.close();
            String result = Base64.encodeBase64String(output.toByteArray());
            output.close();
            return result;
        } catch (IOException e) {

        }
        return null;
    }
}
